<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>项目详情</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css">
</head>
<body>
    <div class="container mt-4">
        <div class="row mb-3">
            <div class="col">
                <h2>项目详情</h2>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="/risk-management/project/list">项目列表</a></li>
                        <li class="breadcrumb-item active" aria-current="page">项目详情</li>
                    </ol>
                </nav>
            </div>
        </div>
        
        <div class="row mb-3">
            <div class="col">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">项目信息</h5>
                        <div>
                            <a th:href="@{/risk-management/project/edit/{id}(id=${project.id})}" class="btn btn-sm btn-primary" sec:authorize="hasAnyRole('ADMIN', 'MANAGER')">
                                <i class="fa fa-edit"></i> 编辑
                            </a>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <th style="width: 30%">项目名称:</th>
                                        <td th:text="${project.name}"></td>
                                    </tr>
                                    <tr>
                                        <th>项目状态:</th>
                                        <td>
                                            <span th:if="${project.status == 1}" class="badge badge-secondary">未开始</span>
                                            <span th:if="${project.status == 2}" class="badge badge-primary">进行中</span>
                                            <span th:if="${project.status == 3}" class="badge badge-success">已完成</span>
                                            <span th:if="${project.status == 4}" class="badge badge-danger">已取消</span>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>项目经理:</th>
                                        <td th:text="${manager != null ? manager.realName : '未指定'}"></td>
                                    </tr>
                                </table>
                            </div>
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <th style="width: 30%">开始日期:</th>
                                        <td th:text="${project.startDate != null ? #dates.format(project.startDate, 'yyyy-MM-dd') : '未设置'}"></td>
                                    </tr>
                                    <tr>
                                        <th>结束日期:</th>
                                        <td th:text="${project.endDate != null ? #dates.format(project.endDate, 'yyyy-MM-dd') : '未设置'}"></td>
                                    </tr>
                                    <tr>
                                        <th>创建时间:</th>
                                        <td th:text="${#dates.format(project.createTime, 'yyyy-MM-dd HH:mm:ss')}"></td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-12">
                                <h6>项目描述:</h6>
                                <p th:text="${project.description != null && !project.description.isEmpty() ? project.description : '暂无描述'}"></p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="row mb-3">
            <div class="col">
                <div class="card">
                    <div class="card-header">
                        <ul class="nav nav-tabs card-header-tabs">
                            <li class="nav-item">
                                <a class="nav-link active" href="#" id="tab-risks">
                                    <i class="fa fa-exclamation-triangle"></i> 风险列表
                                </a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link" th:href="@{/project-member/list/{id}(id=${project.id})}">
                                    <i class="fa fa-users"></i> 项目成员
                                </a>
                            </li>
                        </ul>
                    </div>
                    <div class="card-body">
                        <div class="d-flex justify-content-between mb-3">
                            <h5>项目风险</h5>
                            <a th:href="@{/risk-management/risk/add(projectId=${project.id})}" class="btn btn-sm btn-success">
                                <i class="fa fa-plus"></i> 添加风险
                            </a>
                        </div>
                        <div id="risks-container">
                            <div class="text-center py-3">
                                <i class="fa fa-spinner fa-spin fa-2x"></i>
                                <p class="mt-2">加载风险数据...</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="row" sec:authorize="hasAnyRole('ADMIN', 'MANAGER')">
            <div class="col">
                <div class="card">
                    <div class="card-header">项目管理</div>
                    <div class="card-body">
                        <div class="btn-group">
                            <button type="button" class="btn btn-primary" onclick="changeStatus(2)" th:if="${project.status == 1}">
                                <i class="fa fa-play"></i> 开始项目
                            </button>
                            <button type="button" class="btn btn-success" onclick="changeStatus(3)" th:if="${project.status == 2}">
                                <i class="fa fa-check"></i> 完成项目
                            </button>
                            <button type="button" class="btn btn-danger" onclick="changeStatus(4)" th:if="${project.status != 4}">
                                <i class="fa fa-times"></i> 取消项目
                            </button>
                            <button type="button" class="btn btn-danger" onclick="confirmDelete()" th:if="${project.status == 1 || project.status == 4}">
                                <i class="fa fa-trash"></i> 删除项目
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- 删除确认模态框 -->
    <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="deleteModalLabel">确认删除</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <p>确定要删除项目 <span th:text="${project.name}"></span> 吗？此操作不可恢复！</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-danger" onclick="deleteProject()">删除</button>
                </div>
            </div>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
    <script th:inline="javascript">
        $(document).ready(function() {
            loadRisks();
        });
        
        // 加载风险数据
        function loadRisks() {
            const projectId = [[${project.id}]];
            $.ajax({
                url: '/risk-management/risk/project/' + projectId,
                type: 'GET',
                dataType: 'html',
                success: function(response) {
                    const container = $(response).find('.table-responsive');
                    if (container.length > 0) {
                        $('#risks-container').html(container);
                    } else {
                        $('#risks-container').html('<p class="text-center">暂无风险数据</p>');
                    }
                },
                error: function() {
                    $('#risks-container').html('<p class="text-center text-danger">加载风险数据失败</p>');
                }
            });
        }
        
        // 更改项目状态
        function changeStatus(status) {
            const projectId = [[${project.id}]];
            $.ajax({
                url: '/risk-management/project/status/' + projectId + '/' + status,
                type: 'POST',
                success: function(result) {
                    if (result.code === 200) {
                        alert('项目状态更新成功');
                        location.reload();
                    } else {
                        alert('项目状态更新失败：' + result.message);
                    }
                },
                error: function() {
                    alert('请求失败，请稍后重试');
                }
            });
        }
        
        // 确认删除项目
        function confirmDelete() {
            $('#deleteModal').modal('show');
        }
        
        // 删除项目
        function deleteProject() {
            const projectId = [[${project.id}]];
            $.ajax({
                url: '/risk-management/project/delete/' + projectId,
                type: 'POST',
                success: function(result) {
                    if (result.code === 200) {
                        alert('项目删除成功');
                        window.location.href = '/risk-management/project/list';
                    } else {
                        alert('项目删除失败：' + result.message);
                    }
                },
                error: function() {
                    alert('请求失败，请稍后重试');
                }
            });
        }
    </script>
</body>
</html> 